package com.example.myone.Util;

import android.content.Context;
import android.content.SharedPreferences;

import com.example.myone.Entity.User;
import com.google.gson.Gson;

public class LoginInfoUtil {
    private static final Gson GSON = new Gson();
    private static final String PREF_NAME = "loginInfo";
    private static final String KEY = "user";
    public static User getUser(Context context) {
        SharedPreferences accountPrefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        String json = accountPrefs.getString(KEY, null);
        return json != null ? GSON.fromJson(json, User.class) : null;
    }
    public static void save(Context context, User user) {
        System.out.println("保存用户" + user.account);
        String json = GSON.toJson(user);
        context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
                .edit()
                .putString(KEY, json)
                .apply();
    }
    public static void remove(Context context) {
        context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
                .edit()
                .remove(KEY)
                .apply();
    }
    public static boolean isLogin(Context context) {
        return getUser(context) != null;
    }
}

